package org.example.gui.model;

import org.example.dao.CategoryDAO;
import org.example.dao.RecordDAO;
import org.example.entity.Record;
import org.example.gui.widget.HistoryPickerPanel;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.text.DecimalFormat;
import java.util.List;

/**
 * HistoryTablePanel 的 Table 的数据模型，实现了 TableModel 接口
 */

public class RecordTableModel implements TableModel {
    private final String[] columnNames = new String[]{"花费", "分类", "备注", "地点", "日期"};
    public List<Record> recordList = RecordDAO.listAnyMonthByStartDay(HistoryPickerPanel.instance.date);

    @Override
    public int getRowCount() {
        return recordList.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (0 == columnIndex) {
            DecimalFormat df = new DecimalFormat("0.00");
            return df.format(recordList.get(rowIndex).getSpend());
        }
        if (1 == columnIndex) {
            int cid = recordList.get(rowIndex).getCid();
            return CategoryDAO.getCategoryById(cid).getName();
        }
        if (2 == columnIndex)
            return recordList.get(rowIndex).getComment();
        if (3 == columnIndex) {
            return recordList.get(rowIndex).getLocation();
        }
        if (4 == columnIndex)
            return recordList.get(rowIndex).getDate();

        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {

    }
}
